import { defineStore } from 'pinia'
import { ElMessage } from 'element-plus'
import $axios from '../tools/axios'
// import {useRouter} from 'vue-router'

// const router = useRouter();

const basepath = '/passenger'
export const usePassengerStore = defineStore('passenger', {
  state: () => {
    return {
      passids: [],
      tableData: [],
      checkList: []
    }
  },
  actions: {

    //查询乘车人
    async queryPassenger() {
      this.passids = []
      this.checkList = []
      let user = JSON.parse(sessionStorage.getItem('user'))

      const path = basepath + '/query?userid=' + user.userid

      const { data: res } = await $axios.post(path)

      if (res.code === 200) {
        res.data.forEach((element) => {
          this.passids.push(+element.passid)
        })

        const path2 = '/user/query?passids=' + this.passids

        const { data: res1 } = await $axios.post(path2)

        if (res.code === 200) {
          this.tableData = res1.data
          this.tableData.forEach((elt) => {
            elt.idtype = '中国居民身份证'
            elt.id = elt.id.substring(0, 4) + '***********' + elt.id.substring(15, 18)
            elt.mobile = '(+86)' + elt.mobile.substring(0, 3) + '****' + elt.mobile.substring(7, 11)
          })
        }
      } else {
        ElMessage.error('查询乘车人失败！')
      }
    }
  }
})
